package com.site.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.site.dto.AdvertiseBizBean;
import com.site.service.AdvertiseService;
import com.site.util.Code;
import com.site.util.CodeMapDBUtil;
import com.site.util.StringUtils;
import com.site.util.base.action.BaseAction;
import com.site.util.base.service.BaseService;
import com.site.util.constant.Constants;

/**
 * 广告
 * 
 * @author lee
 * @date 2013年11月30日
 */
public class AdvertiseAction extends BaseAction {

	private static final long serialVersionUID = -7903648681886405714L;

	private AdvertiseBizBean advertiseBizBean;

	private List<AdvertiseBizBean> advertiseBizBeans;
	
	private AdvertiseService advertiseService;
	
	private List<Code> codes;
	
	public List<Code> getCodes() {
		return codes;
	}
	public void setCodes(List<Code> codes) {
		this.codes = codes;
	}
	@Resource
	public void setAdvertiseService(AdvertiseService advertiseService) {
		this.advertiseService = advertiseService;
	}

	public List<AdvertiseBizBean> getAdvertiseBizBeans() {
		return advertiseBizBeans;
	}

	public void setAdvertiseBizBeans(List<AdvertiseBizBean> advertiseBizBeans) {
		this.advertiseBizBeans = advertiseBizBeans;
	}

	public AdvertiseBizBean getAdvertiseBizBean() {
		return advertiseBizBean;
	}

	public void setAdvertiseBizBean(AdvertiseBizBean advertiseBizBean) {
		this.advertiseBizBean = advertiseBizBean;
	}
	
	/**
	 * 根据条件获得广告
	 * @return
	 */
	public String getAdvertiseList(){
		if(null == queryPage){
			super.initPagination();
		}
		advertiseBizBeans = advertiseService.getAdvertiseList(queryPage, advertiseBizBean);
		return "getAdvertiseList";
	}
	
	/**
	 * 广告增加页面
	 * @return
	 */
	public String getAdvertiseAdd(){
		codes = CodeMapDBUtil.getCodeByCodeName(Constants.ADVERTISE_TYPE, request);
		return "getAdvertiseAdd";
	}
	
	/**
	 * 广告增加
	 * @return
	 */
	public String advertiseAdd(){
		
		if(null != advertiseBizBean){
			boolean b = false;
			if(StringUtils.isEmpty(advertiseBizBean.getTitle())){
				super.addFieldError("title","广告名不能为空！");
				b = true;
			}
			if(StringUtils.isEmpty(advertiseBizBean.getUrl())){
				super.addFieldError("url","广告URL名不能为空！");
				b = true;
			}
			if(files == null || files.size() <= 0){
				super.addFieldError("files","未增加图片！");
				b = true;
			}
			if(StringUtils.isEmpty(advertiseBizBean.getType())){
				super.addFieldError("title","广告类型不能为空！");
				b = true;
			}
			if(b){
				return getAdvertiseAdd();
			}
			this.getAdvertiseBizBean().setRequest(request);
			Boolean boolean1 =(Boolean)advertiseService.execute(this, BaseService.COMMAND_ADD);
			if(boolean1){
				super.setMessageID("SYS_MSG_00022");
			} else {
				super.setMessageID("SYS_MSG_00026"); 
			}
			super.setBackurl(request.getContextPath() + "/adver/advertiseAction!getAdvertiseList.do");
			return "toMessage";
		} else {
			return getAdvertiseAdd();
		}
	}
	
	/**
	 * 广告的删除
	 * @return
	 */
	public String advertiseDelete(){
		if(null !=advertiseBizBeans && advertiseBizBeans.size() > 0){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("advertiseBizBeans", advertiseBizBeans);
			map.put("advertiseAction", this);
			advertiseBizBean.setRequest(request);
			advertiseService.execute(map, BaseService.COMMAND_DELETE_ALL);
			super.setMessageID("SYS_MSG_00022");
		} else {
			super.setMessageID("SYS_MSG_00026");
		}
		super.setBackurl(request.getContextPath() + "/adver/advertiseAction!getAdvertiseList.do");
		return "toMessage";
	}
	
	
}
